package dimension.starry.item;

import dimension.starry.StarryDimension;
import dimension.starry.block.BlockRegistryManager;
import dimension.starry.block.machine.simple.SimpleMachineRegister;
import dimension.starry.block.typeInterface.Machine;
import dimension.starry.item.typeInterface.FoodType;
import dimension.starry.item.typeInterface.MaterialType;
import dimension.starry.item.typeInterface.ToolType;
import dimension.starry.item.typeInterface.WeaponType.WeaponType;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.item.*;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.registry.RegistryKey;

public class ItemGroups {

    public static final RegistryKey<ItemGroup> BLOCK = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "block")
    );

    public static final RegistryKey<ItemGroup> MACHINE = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "machine")
    );

    public static final RegistryKey<ItemGroup> MATERIAL = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "material")
    );

    public static final RegistryKey<ItemGroup> FOOD = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "food")
    );

    public static final RegistryKey<ItemGroup> WEAPON = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "weapon")
    );

    public static final RegistryKey<ItemGroup> TOOL = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "tool")
    );

    public static final RegistryKey<ItemGroup> ARMOR = RegistryKey.of(
        RegistryKeys.ITEM_GROUP, new Identifier(StarryDimension.MOD_ID, "armor")
    );

    public static void onInitialize() {
        Registry.register(Registries.ITEM_GROUP, BLOCK, FabricItemGroup.builder()
            .icon(() -> new ItemStack(BlockRegistryManager.SALT_ORE.getBlock()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.block"))
            .build());
        Registry.register(Registries.ITEM_GROUP, MACHINE, FabricItemGroup.builder()
            .icon(() -> new ItemStack(SimpleMachineRegister.SIMPLE_FURNACE.getItem()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.machine"))
            .build());
        Registry.register(Registries.ITEM_GROUP, MATERIAL, FabricItemGroup.builder()
            .icon(() -> new ItemStack(ItemRegisterManager.SALT.getItem()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.material"))
            .build());
        Registry.register(Registries.ITEM_GROUP, FOOD, FabricItemGroup.builder()
            .icon(() -> new ItemStack(ItemRegisterManager.SHIT.getItem()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.food"))
            .build());
        Registry.register(Registries.ITEM_GROUP, WEAPON, FabricItemGroup.builder()
            .icon(() -> new ItemStack(ItemRegisterManager.FIRST_SWORD.getItem()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.weapon"))
            .build());
        Registry.register(Registries.ITEM_GROUP, TOOL, FabricItemGroup.builder()
            .icon(() -> new ItemStack(ItemRegisterManager.KETTLE.getItem()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.tool"))
            .build());
        Registry.register(Registries.ITEM_GROUP, ARMOR, FabricItemGroup.builder()
            .icon(() -> new ItemStack(ItemRegisterManager.FIRST_HELMET.getItem()))
            .displayName(Text.translatable(StarryDimension.MOD_ID + ".item_group.armor"))
            .build());
    }

    public static void registerToGroup(Item item) {
        if (item instanceof FoodType) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.FOOD).register(content -> content.add(item));
        } else if (item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof Machine) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.MACHINE).register(content -> content.add(item));
        } else if (item instanceof BlockItem) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.BLOCK).register(content -> content.add(item));
        } else if (item instanceof MaterialType) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.MATERIAL).register(content -> content.add(item));
        } else if (item instanceof ToolType) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOL).register(content -> content.add(item));
        } else if (item instanceof WeaponType) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.WEAPON).register(content -> content.add(item));
        } else if (item instanceof ArmorItem) {
            ItemGroupEvents.modifyEntriesEvent(ItemGroups.ARMOR).register(content -> content.add(item));
        }
    }

}
